part of 'view.dart';

/// 时间间隔：今日、本周、本月、本年
final _alarmInterval = ValueNotifier(_AlarmInterval.day)..addListener(_getAlarmHistory);

/// 选择时间间隔
Future<void> _showIntervalPicker() async {
  final intervals = List<_AlarmInterval>.from(_AlarmInterval.values)..remove(_AlarmInterval.week);
  final picker = ListView.builder(
    itemCount: intervals.length,
    itemBuilder: (_, i) {
      final interval = intervals[i];
      return ListTile(
        title: Text(interval.title),
        onTap: () {
          _alarmInterval.value = interval;
          Get.back();
        },
      );
    },
  );
  await Get.bottomSheet(
    Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(13.w)),
      ),
      height: 300.w,
      child: picker,
    ),
  );
}

enum _AlarmInterval {
  day,
  week,
  month,
  year,
  ;

  String get title {
    switch (this) {
      case _AlarmInterval.day:
        return '今日';
      case _AlarmInterval.week:
        return '本周';
      case _AlarmInterval.month:
        return '本月';
      case _AlarmInterval.year:
        return '本年';
    }
  }

  /// 时间间隔从开始到结束String
  (String, String) get startToEnd {
    final now = DateTime.now();
    switch (this) {
      case _AlarmInterval.day:
        return (now.subtract(const Duration(days: 1)).toStr, now.toStr);
      case _AlarmInterval.week:
        final weekDay = now.weekday;
        final start = now.subtract(Duration(days: weekDay - 1));
        final end = now.add(Duration(days: 7 - weekDay));
        return (start.toStr, end.toStr);
      case _AlarmInterval.month:
        final start = DateTime(now.year, now.month, 1);
        final end = DateTime(now.year, now.month + 1, 0);
        return (start.toStr, end.toStr);
      case _AlarmInterval.year:
        final start = DateTime(now.year, 1, 1);
        final end = DateTime(now.year, 12, 31);
        return (start.toStr, end.toStr);
    }
  }
}

extension _WidgetX on Widget {
  Widget get _wrapSized {
    return SizedBox(
      width: 137.w,
      child: this,
    );
  }
}
